<template>
    <navBar title="我的订单" borBot tabBar></navBar>
    <view class="TypeBox">
        <view
            class="list"
            v-for="(item, index) in TypeData"
            :key="index"
            @click="screenBtn(item, index)">
            <text class="title">{{ item.content ? item.content : item.name }}</text>
            <view class="iconfont icon-xiangxia"></view>
            <up-picker
                :show="item.show"
                :columns="item.columns"
                :keyName="item.keyName"
                @cancel="item.show = false"
                @confirm="confirmBtn(item, index, $event)"></up-picker>
        </view>
    </view>

    <view class="Search">
        <view class="iconfont icon-sousuo color-333 h7"></view>
        <input
            type="text"
            v-model="content"
            class="searchTxt"
            @confirm="serachConfirm"
            placeholder="请输入关键字进行搜索" />
    </view>

    <view class="contLis" v-for="(item, index) in pageData" :key="index" @click="toChat(item)">
        <view class="List">
            <view class="lis">
                <text class="name">委托人 :</text>
                <text class="name_content">{{ item.userName }}</text>
            </view>
            <view class="lis">
                <text class="name">联系电话 :</text>
                <text class="name_content">{{ item.phone }}</text>
            </view>
            <view class="lis">
                <text class="name">订单类型 :</text>
                <text class="name_content">{{ item.typeName }}</text>
            </view>
        </view>
        <view class="contact" v-if="item.lastMessage && item.lastMessage.messageType == 0">
            <image
                :src="imgUrls(mainStore.userInfo.avatar)"
                mode="aspectFill"
                class="avatar"></image>
            <text class="flex-1 ellipsis_1 black size26">{{ item.lastMessage.content }}</text>
            <text class="color-999 size26 p-left20">{{
                tampToDate(item.lastMessage.sendTime)
            }}</text>
        </view>
    </view>

    <view class="rows m-top100" v-if="!pageData.length">
        <up-empty mode="list" text="暂无相关内容~"></up-empty>
    </view>

    <up-datetime-picker
        :show="show"
        v-model="TypeData[1].value1"
        :maxDate="TypeData[1].maxDate"
        mode="date"
        @cancel="show = false"
        @confirm="timeConfirm"></up-datetime-picker>

    <TabBar :tab="1"></TabBar>
</template>

<script setup>
    import TabBar from './components/TabBar.vue';
    import Http from '@/utils/request.js';
    import {
        toUrl,
        Toast,
        staticImg,
        imgUrls,
        Call,
        tampToDate,
        showLoading,
        hideLoading,
    } from '@/utils/utils.js';
    import { ref, computed, watch } from 'vue';
    import { getByType } from '@/utils/api.js';
    import {
        onLoad,
        onShow,
        onReachBottom,
        onShareAppMessage,
        onShareTimeline,
    } from '@dcloudio/uni-app';
    import { useMainStore } from '@/stores/index';

    const mainStore = useMainStore();
    const pageNum = ref(1);
    const pageSize = ref(10);
    const getMore = ref(true);
    const pageData = ref([]);
    const content = ref(''); //搜索
    const show = ref(false); // 时间弹窗
    const TypeData = ref([
        {
            name: '订单类型',
            columns: [],
            content: '',
            orderType: '',
            show: false,
            keyName: 'dictLabel',
        },
        {
            name: '时间',
            columns: [],

            content: '',
            value1: Date.now(),
            maxDate: Date.now(),
        },
        {
            name: '订单状态',
            columns: [],
            content: '',
            orderState: '',
            show: false,
            keyName: 'dictLabel',
        },
    ]);

    onLoad(async () => {
        order();
        const resultType = await getByType('order_type');
        const resultState = await getByType('order_state');

        TypeData.value[0].columns[0] = resultType;
        TypeData.value[2].columns[0] = resultState;
    });

    const order = () => {
        showLoading('');
        Http.post({
            url: `/order/listAndIM?pageNum=${pageNum.value}&pageSize=${pageSize.value}&createTime=${TypeData.value[1].content}&state=${TypeData.value[2].orderState}&orderType=${TypeData.value[0].orderType}&content=${content.value}&lawyerUserId=${mainStore.userInfo.userId}`,
            // data: {
            // 	createTime: TypeData.value[1].content,
            // 	state: TypeData.value[2].orderState,
            // 	orderType: TypeData.value[0].orderType,
            // 	content: content.value,
            // 	userId: mainStore.userInfo.userId,
            // 	lawyerUserId: mainStore.userInfo.userId,
            // },
        }).then((res) => {
            hideLoading();
            if (res.code != 0) return Toast(res.msg);
            const result = res.rows || [];
            result.forEach((item) => {
                if (item.lastMessage && item.lastMessage.extend) {
                    item.lastMessage.extend = JSON.parse(item.lastMessage.extend);
                }
            });

            pageNum.value == 1
                ? (pageData.value = result)
                : (pageData.value = [...pageData.value, ...result]);
            if (result.length < pageSize.value) return (getMore.value = false);
        });
    };

    const screenBtn = (Item, Index) => {
        if (Item.keyName) return (Item.show = true);
        show.value = true;
    };

    const confirmBtn = (Item, Index, event) => {
        TypeData.value[Index].content = event.value[0].dictLabel;
        Index == 2
            ? (TypeData.value[Index].orderState = event.value[0].dictValue)
            : (TypeData.value[Index].orderType = event.value[0].dictValue);
        Item.show = false;
        pageNum.value = 1;
        getMore.value = true;
        order();
    };

    const timeConfirm = (event) => {
        const resultTime = tampToDate(event.value);
        TypeData.value[1].content = resultTime;
        show.value = false;
        pageNum.value = 1;
        getMore.value = true;
        order();
    };

    const serachConfirm = () => {
        pageNum.value = 1;
        getMore.value = true;
        order();
    };

    const toChat = (Item) => {
        if (Item.id) toUrl(`/pages/chat/chat?chatId=${Item.id}`);
    };

    onReachBottom(() => {
        if (!getMore.value) return;
        pageNum.value++;
        order();
    });
</script>
<style lang="scss">
    page {
        background-color: $page;
        padding-bottom: calc(120rpx + constant(safe-area-inset-bottom));
        padding-bottom: calc(120rpx + env(safe-area-inset-bottom));
    }
</style>
<style lang="scss" scoped>
    .TypeBox {
        width: 100%;
        background-color: #fff;
        height: 96rpx;
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        .list {
            height: 100%;
            display: flex;
            align-items: center;
            justify-content: center;
            .title {
                max-width: 180rpx;
                color: #666666;
                font-size: 28rpx;
                overflow: hidden;
                text-overflow: ellipsis;
                display: -webkit-box;
                box-orient: vertical;
                -webkit-box-orient: vertical;
                line-clamp: 1;
                -webkit-line-clamp: 1;
            }
            .icon-xiangxia {
                font-size: 24rpx;
                color: #999999;
                padding-left: 10rpx;
            }
        }
    }
    .Search {
        width: 710rpx;
        height: 70rpx;
        background-color: #fff;
        border-radius: 35rpx;
        margin: 20rpx auto;
        padding: 0 24rpx;
        display: flex;
        align-items: center;
        .searchTxt {
            flex: 1;
            height: 100%;
            padding-left: 20rpx;
            font-size: 30rpx;
            color: #333;
        }
    }

    .contLis {
        width: 710rpx;
        display: flex;
        flex-direction: column;
        align-items: center;
        margin: 20rpx auto 0;
        border-radius: 10rpx;
        overflow: hidden;
        .List {
            width: 100%;
            background-color: #fff;
            padding: 20rpx 20rpx 0 20rpx;
            position: relative;
            .lis {
                display: flex;
                padding-bottom: 20rpx;
                .name {
                    width: 180rpx;
                    color: #999999;
                    font-size: 28rpx;
                    padding-right: 20rpx;
                }
                .name_content {
                    flex: 1;
                    color: #000;
                    font-size: 28rpx;
                }
            }
        }
        .contact {
            width: 100%;
            padding: 20rpx;
            border-top: 1rpx solid #f1f1f1;
            background-color: #fff;
            display: flex;
            align-items: center;
            .avatar {
                width: 80rpx;
                height: 80rpx;
                border-radius: 50%;
                margin-right: 20rpx;
            }
        }
    }
</style>
